<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link href="../bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
		<title></title>
	</head>
	<body>
		<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
		  <div class="modal-dialog" role="document">
		    <div class="modal-content">
		      <div class="modal-header">
		        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
		        <h4 class="modal-title" style="text-align: center;">新用户注册</h4>
		      </div>
		      <div class="modal-body">
		       <form class="form-horizontal">
				   <div class="form-group" style="text-align: center;">
					   <label class="radio-inline">
						 <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="administrators"> 宿舍管理员
					   </label>
					   <label class="radio-inline">
						 <input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="student"> 学生
					   </label>
				   </div>
				  <div class="form-group">
				      <label for="inputEmail3" class="col-sm-2 control-label">用户名</label>
				      <div class="col-sm-10">
				        <input type="text" class="form-control" id="n_user" placeholder="用户名或者学号">
				      </div>
				    </div> 
					  <div class="form-group ">
					      <label for="inputEmail3" class="col-sm-2 control-label">电话号码</label>
					      <div class="col-sm-10">
					        <input type="text" class="form-control" id="Telephone" placeholder="请输入电话号码">
					      </div>
					    </div>
						<div class="form-group ">
						    <label for="inputEmail3" class="col-sm-2 control-label">密码</label>
						    <div class="col-sm-10">
						      <input type="password" class="form-control" id="password" placeholder="请设置密码">
						    </div>
						  </div>
						  <div class="form-group ">
						      <label for="inputEmail3" class="col-sm-2 control-label">确认密码</label>
						      <div class="col-sm-10">
						        <input type="password" class="form-control" id="se_password" placeholder="再次输入密码">
						      </div>
						    </div>
			   </form>
		      </div>
		      <div class="modal-footer">
		        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
		        <button id="sava" type="button" class="btn btn-primary" data-dismiss="modal">Save changes</button>
		      </div>
		    </div><!-- /.modal-content -->
		  </div><!-- /.modal-dialog -->
		</div><!-- /.modal -->
		<!-- 模态 -->
		<button id="mot" type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
		  Launch demo modal
		</button>
		<script src="../js/jquery-3.5.1.js"></script>
		<script src="../bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				let RadOp=null;
					$("input[name='inlineRadioOptions']").change(function(event){
						 RadOp = $(this).val();
					})
				$("button[id='sava']").click(function(event){
					let n_user=$("input[id='n_user']").val();
					let Telephone=$("input[id='Telephone']").val();
					let password=$("input[id='password']").val();
					let se_password=$("input[id='se_password']").val();
					if (RadOp==="student") {
						let studentarr=JSON.parse(localStorage.getItem("studentarr"));
						if (!studentarr) {
							studentarr=new Array();
						};
						if(n_user.trim().length===0){
							alert("请输入账号");
							return;
						}
						if(password.trim().length<6){
							alert("密码至少要求6位");
							return;
						}
						if (password!==se_password) {
							alert("两次密码必须一致");
							return;
						}
						for (let index in studentarr) {
							let item=studentarr[index];
							if (item.name===n_user) {
								alert("该账号以被注册");
								return;
							}
						}
						let tem=0;
							for (let item of studentarr) {
								let id=item.id;
								if (tem<id) {
									tem=id;
								}
							}
						let obj = {
							id: tem+1,
							name: n_user,
							Telephone: Telephone,
							password: password
						}
						console.log(obj);
						studentarr.push(obj);
						localStorage.setItem("studentarr", JSON.stringify(studentarr));//保存到localStorage中
						alert("注册成功");
					} else if(RadOp="administrators"){
						let adminarr=JSON.parse(localStorage.getItem("adminarr"));
						if (!adminarr) {
							adminarr=new Array();
						};
						if(n_user.trim().length===0){
							alert("请输入账号");
							return;
						}
						if(password.trim().length<6){
							alert("密码至少要求6位");
							return;
						}
						if (password!==se_password) {
							alert("两次密码必须一致");
							return;
						}
						for (let index in adminarr) {
							let item=adminarr[index];
							if (item.name===n_user) {
								alert("该账号以被注册");
								return;
							}
						}
						let tem=0;
							for (let item of adminarr) {
								let id=item.id;
								if (tem<id) {
									tem=id;
								}
							}
						let obj = {
							id: tem+1,
							name: n_user,
							Telephone: Telephone,
							password: password
						}
						console.log(obj);
						adminarr.push(obj);
						localStorage.setItem("adminarr", JSON.stringify(adminarr));//保存到localStorage中
						alert("注册成功");
					}
					console.log(se_password,password,Telephone);
				});
			})
		</script>
	</body>
</html>
